#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <unistd.h>

#define ROOT_DEBUG

#ifdef ROOT_DEBUG
#include <time.h>
#endif
char* host = "appservice.lenovomm.com";
int port = 80;


/*
 *  http://appservice.lenovomm.com/lenovo-bless/common/root/backup?imei=123457&version=1.1.15&kernelversion=1
 *  GET /lenovo-bless/common/root/backup?imei=123457&version=1.1.15&kernelversion=1 HTTP/1.1
 */
//GET /lenovo-bless/common/root/backup?imei=123457&version=1.1.15&kernelversion=1 HTTP/1.1
//Host: appservice.lenovomm.com
//User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:25.0) Gecko/20100101 Firefox/25.0
//Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
//Accept-Language: zh-cn,en-us;q=0.8,zh;q=0.5,en;q=0.3
//Accept-Encoding: gzip, deflate
//DNT: 1
//Cookie: LenovoID.UN=491804300@qq.com; LPSState=1
//Connection: keep-alive
//
//HTTP/1.1 200 OK
//Server: nginx/1.5.2
//Date: Wed, 13 Nov 2013 07:49:45 GMT
//Content-Type: application/json;charset=utf-8
//Content-Length: 20
//Connection: Keep-Alive
//
//{"ret":0,"msg":"ok"}
static int process_status_code(char * msg);
int main(void) {
	char buffer[512];
	int isock;
	struct sockaddr_in pin;
	struct hostent * remoteHost;
	char message[512];
	int done = 0;
	int chars = 0;
	int l = 0;
	int receivecount =0;
	char receivemsg[1024];
	char myimei[20]={0};
	char myversion[50]={0};
	char mykernelversion[50]={0};
	int status_code = 0;

#ifdef ROOT_DEBUG
	time_t timep;
	time (&timep);
	sprintf(myimei,"%s","hello");
	sprintf(myversion,"%s","cgmmyversion");
	sprintf(mykernelversion,"%s","cgmkernel");
#endif


	if ((remoteHost = gethostbyname(host)) == NULL) {
		printf("Error resolving host\n");
		exit(1);
	}
#ifdef ROOT_DEBUG
	printf("remoteHost:%s\n",remoteHost->h_name);
#endif
	bzero(message, sizeof(message));
	bzero(&pin, sizeof(pin));
	pin.sin_family = AF_INET;
	pin.sin_port = htons(port);
	pin.sin_addr.s_addr = ((struct in_addr *) (remoteHost->h_addr) )->s_addr;

	if ((isock = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
		printf("Error opening socket!\n");
		exit(1);
	}
	sprintf(message, "GET /lenovo-bless/common/root/backup?imei=%s&version=%s&kernelversion=%s HTTP/1.1\n",myimei,myversion,mykernelversion);
	strcat(message, "Host:appservice.lenovomm.com\r\n");
	//strcat(message, "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n");
	//strcat(message, "User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:25.0) Gecko/20100101 Firefox/25.0\r\n");
	strcat(message, "connection:keep-alive\r\n");
	//strcat(message,"Cookie: LenovoID.UN=491804300@qq.com; LPSState=1\r\n");
	strcat(message, "\r\n\r\n");

	printf("%s", message);
	while (1) {
		if (connect(isock, (void *) &pin, sizeof(pin)) == -1) {
			printf("Error connecting to socket\n");
			exit(1);
		}

		if (send(isock, message, strlen(message), 0) == -1) {
			printf("Error in send\n");
			exit(1);
		}

		receivecount = recv(isock, receivemsg, 1024, 0);
		if (receivecount != 0) {
#ifdef ROOT_DEBUG
			printf("return message:%s\n", receivemsg);
#endif
			status_code = process_status_code(receivemsg);
			printf("receive status_code:%d\n", status_code);

			switch (status_code) {
			case 200:
				printf("everything goes well,i will exit\n");
				exit(0);
				break;
			default:
				sleep(3000);
				break;
			}
		} else {
			printf("receive error\n");
			sleep(3000);
		}
	}
	close(isock);
	return 0;
}


static int process_status_code(char * msg)
{
	int i,j;
	char num[50];

	for(i = 0; msg[i] != ' '; i++);
	i++;
	for(j = 0; msg[i] != ' '; i++,j++)
		num[j] = msg[i];
	num[j] = '\0';

	return  atoi(num);
}
